package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.CategoryEntity;
import com.sky.entity.DishEntity;
import com.sky.enumeration.CategoryStatusEnum;
import com.sky.enumeration.CategoryTypeEnum;
import com.sky.exceptionHandler.ServiceException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public List<CategoryEntity> page(Integer page, Integer size, String name, CategoryTypeEnum type) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }
        PageHelper.startPage(page, size);
        List<CategoryEntity> list = categoryMapper.getByNameAndType(name, type);
        PageInfo<CategoryEntity> pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }

    @Override
    public void del(Long id) {
        Integer dishCount = dishMapper.countByCategoryId(id);
        if (dishCount > 0) {
            throw new ServiceException("当前分类关联了菜品，不能删除");
        }

        Integer setmealCount = setmealMapper.countByCategoryId(id);
        if (setmealCount > 0) {
            throw new ServiceException("当前分类关联了套餐，不能删除");
        }

        categoryMapper.del(id);
    }

    @Override
    public List<CategoryEntity> getByType(CategoryTypeEnum typeEnum) {
        if (typeEnum == null) {
            List<CategoryEntity> list = categoryMapper.selectAll();
            return list;
        }
        List<CategoryEntity> list = categoryMapper.getByType(typeEnum);
        return list;
    }

    @Override
    public void save(CategoryDTO dto) {
        CategoryEntity entity = new CategoryEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setStatus(CategoryStatusEnum.Enable);
//        LocalDateTime now = LocalDateTime.now();
//        entity.setCreateTime(now);
//        entity.setUpdateTime(now);
//        entity.setCreateUser(BaseContext.getId());
//        entity.setUpdateUser(BaseContext.getId());
        categoryMapper.save(entity);
    }

    @Override
    public void update(CategoryDTO dto) {
        CategoryEntity entity = new CategoryEntity();
        BeanUtils.copyProperties(dto, entity);
//        entity.setUpdateTime(LocalDateTime.now());
//        entity.setUpdateUser(BaseContext.getId());
        categoryMapper.update(entity);
    }

    @Override
    public void enableOrDisable(CategoryStatusEnum statusEnum, Long id) {
        CategoryEntity entity = new CategoryEntity();
        entity.setStatus(statusEnum);
        entity.setId(id);
//        entity.setUpdateUser(BaseContext.getId());
//        entity.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(entity);
    }
}
